package com.ctgu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ctgu.common.ResponseResult;
import com.ctgu.model.entity.Company;
import com.ctgu.model.entity.Employee;
import com.ctgu.model.entity.LoginUser;
import com.ctgu.service.ICompanyService;
import com.ctgu.service.IEmployeeService;
import com.ctgu.service.impl.EmployeeServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yiMa
 * @since 2023-07-05
 */
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private EmployeeServiceImpl employeeServiceImpl;

    @GetMapping("/one")
    @PreAuthorize("@AYM.hasAuthority('employee:info:query')")
    public ResponseResult info(Integer id){

        return employeeService.getEmployeeOne(id);
    }

    /**
     * 公司管理员查看员工信息-分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    @PreAuthorize("@AYM.hasAuthority('employee:info:page')")
    public ResponseResult page(int page, int pageSize, String name,Integer depId){
        return employeeService.getList(page,pageSize,name,depId);
    }

    @PostMapping("/register")
    public ResponseResult register(@RequestBody Employee employee){
        return employeeService.register(employee);
    }

    //更改个人信息
    @PutMapping("/updateInfo")
    @PreAuthorize("@AYM.hasAuthority('employee:info:update')")
    public ResponseResult updateInfo(@RequestBody Employee employee){
//        LoginUser loginUser = (LoginUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
//        Integer id = loginUser.getUser().getId();
//        QueryWrapper queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("id",id);
        return employeeServiceImpl.updateEmpInfo(employee);
    }
    //hr查看非在职人才
    @GetMapping("/getDepartPage")
    public ResponseResult getJobEmp(int page, int pageSize,String name){
        return employeeService.getDepartList(page,pageSize,name);
    }

    @PostMapping("/addCompany")
    @PreAuthorize("@AYM.hasAuthority('employee:one:addCompany')")
    public ResponseResult addCompany(@RequestBody Employee employee){
        return employeeService.addCompany(employee);
    }

    @PutMapping("/moveEmployee")
    @PreAuthorize("@AYM.hasAuthority('employee:position:update')")
    public ResponseResult moveEmployee(int id,int toDepId,int toPositionId){
        return employeeService.moveEmployee(id,toDepId,toPositionId);
    }

}

